﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Commonlib;
using Albon.Model;

namespace DAL
{
    public class InvoiceDAL
    {
        //Invoice

        /// <summary>
        /// 获取订单的应付款数
        /// </summary>
        public DataTable getInvoice(string invoiceNo)
        {
            string cmdstr = "select * from InvoiceView where invoiceNo="+invoiceNo;
            return SqlHelper.ExecuteDataSet(SqlHelper.SqlConnectionString,CommandType.Text,cmdstr,null).Tables[0];
        }

        /// <summary>
        /// 添加/修改invoice
        /// </summary>
        public bool alterInvoice(Invoice invoice)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@invoiceNo",SqlDbType.Int,4),
					new SqlParameter("@payMethod", SqlDbType.VarChar,50)};
            parameters[0].Value = invoice.invoiceNo;
            parameters[1].Value = invoice.payMethod;
            return SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString,CommandType.StoredProcedure,"p_invoicealter",parameters)>0;
        }

        /// <summary>
        /// 计算应付款
        /// </summary>
        public bool totalMoney(Invoice invoice)
        {
            SqlParameter[] parameters = new SqlParameter[] { 
                new SqlParameter("@invoiceNo",invoice.invoiceNo),
                new SqlParameter("@invoicePath",invoice.invoicePath),
                new SqlParameter("@invoiceTotal",invoice.invoiceTotal),
                new SqlParameter("@payMethod",invoice.payMethod),
				new SqlParameter("@totalMoney", invoice.totalMoney),
				new SqlParameter("@adder",invoice.adder)
            };
            return SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString,CommandType.StoredProcedure,"p_invoicetotal",parameters)>0;
        }

        //获取发票对应的附加（计划外）费用
        public DataTable getExtras(string invoiceNo)
        {
            string cmdstr = "select * from ExtraCharge where invoiceNo=" + invoiceNo;
            return SqlHelper.ExecuteDataSet(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null).Tables[0];
        }
        //添加额外收费信息
        public void addExtraCharge(ExtraCharge extra)
        {
            SqlParameter[] parameters = new SqlParameter[] { 
                new SqlParameter("@invoiceNo",extra.invoiceNo),
                new SqlParameter("@extraCharge",extra.extraCharge),
                new SqlParameter("@remark",extra.remark)
            };
            SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString, CommandType.StoredProcedure, "p_extraadd", parameters);
        }

        public void deleteExtraCharge(string id)
        {
            string cmdstr = "delete ExtraCharge where ID="+id;
            SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
        }
    }
}
